//
//  SLSearchViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/6/10.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLSearchViewController.h"
#import "SLFriendListTableViewCell.h"
#import "SLUserSeacherViewModel.h"
#import "SLHisBoatsViewController.h"
#import "SLHisGoodsViewController.h"





@interface SLSearchViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate>

@property (nonatomic, strong) UITableView *resultTableView;

@property (nonatomic, strong) UISearchController *searchController;

@property (nonatomic, strong) SLUserSeacherViewModel *searchModel;
@end

@implementation SLSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  
  [self.view addSubview:self.resultTableView];
  self.resultTableView.tableHeaderView = self.searchController.searchBar;
  [self.resultTableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(3);
    make.bottom.equalTo(self.mas_bottomLayoutGuideTop).offset(-3);
    make.width.centerX.equalTo(self.view);
  }];
  
  @weakify(self)
  [[RACObserve(self.searchModel, userList) filter:^BOOL(NSMutableArray *value) {
    return value != nil;
  }] subscribeNext:^(id x) {
    @strongify(self)
    [self.resultTableView reloadData];
//    self.searchController.active = NO;
  }];
  
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  self.title = @"搜索用户";
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  self.searchController.active = NO;
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.searchModel.userList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 100;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  SLFriendListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(SLFriendListTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  [cell configFriendListCellWithModel:self.searchModel.userList[indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  SLFriendModel *model = self.searchModel.userList[indexPath.row];
  
  if (model.type == 0) {
    SLHisBoatsViewController *boatsVC = [SLHisBoatsViewController new];
    [boatsVC getMoreBoatWithHid:[NSString stringWithFormat:@"%ld",(long)model.fid]];
    [self.navigationController pushViewController:boatsVC animated:YES];
  }else{
    SLHisGoodsViewController *goodsVC = [SLHisGoodsViewController new];
    [goodsVC changeHid:[NSString stringWithFormat:@"%ld",(long)model.fid]];
    [self.navigationController pushViewController:goodsVC animated:YES];
  }

}



- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
  
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  
  if(searchBar.text.length >0){
    NSString *string = searchBar.text;
    
    NSString *first = [string substringWithRange:NSMakeRange(0, 1)];
    if (first.integerValue) {
      if(string.length > 3){
        [self.searchModel searchWithKeyWord:string page:@1];
        return;
      }
    }else{
      
      if (string.length > 1) {
        [self.searchModel searchWithKeyWord:string page:@1];
        return;
      }
      
    }
  }
  [SVProgressHUD showErrorWithStatus:@"信息长度无法支持搜索"];
  
}





- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  return [[NSAttributedString alloc] initWithString:@"暂无用户" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}


#pragma mark - getter

- (SLUserSeacherViewModel *)searchModel{
  if (!_searchModel) {
    _searchModel  = [[SLUserSeacherViewModel alloc] init];
  }
  return _searchModel;
}

- (UITableView *)resultTableView{
  if (!_resultTableView) {
    _resultTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _resultTableView.tableFooterView = [UIView new];
    _resultTableView.emptyDataSetSource = self;
    _resultTableView.delegate = self;
    _resultTableView.dataSource = self;
    _resultTableView.backgroundColor = [UIColor defeutBackgroudColor];
    [_resultTableView registerNib:[UINib nibWithNibName:@"SLFriendListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
  }
  return _resultTableView;
}


- (UISearchController *)searchController{
  if (!_searchController) {
    _searchController = [[UISearchController alloc] initWithSearchResultsController:NULL];
    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = false;
    _searchController.searchBar.backgroundColor = [UIColor whiteColor];
    _searchController.searchBar.tintColor = SLYellowColor;
    _searchController.searchBar.translucent = NO;
    _searchController.searchBar.barTintColor = [UIColor whiteColor];
    _searchController.searchBar.delegate = self;
    _searchController.searchBar.placeholder = @"用户姓名或者用户账号";
    [_searchController.searchBar setSearchFieldBackgroundImage:[self searchBarBackFillImage] forState:UIControlStateNormal];
    
  }
  return _searchController;
}



- (UIImage *)searchBarBackFillImage{
  CGSize size = CGSizeMake(30, 30);
  // create context with transparent background
  UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  
  // Add a clip before drawing anything, in the shape of an rounded rect
  [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,30,30)
                              cornerRadius:5.0] addClip];
  [[UIColor defeutBackgroudColor] setFill];
  
  UIRectFill(CGRectMake(0, 0, size.width, size.height));
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  
  return image;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
